--
-- Author: ping.wang
-- Date: 2019-05-15 12:36:22
--
-- DailyTimelinePanel
--

local ClassRef = app.mvc.createPanelClass(...)
local DailyworkflowRender = import("..views.renders.DailyworkflowRender")
local DailyTimelineBlockRender = import("..views.renders.DailyTimelineBlockRender")

--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.DailyTimelinePanel))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)
    self:setContentSize( self._ui.Panel:getContentSize() )

    local scrollView = self._ui.ScrollView
    local timelineSubkeyInfo = GD:getTimeLineSubkeyData()


    local num = 0
    self._allList = {}
    self._timePoint = 0 --时间标识 这里由于策划的配置表 你可能得到完全不在某个时间区域内,但是你还是需要一个标识知道某个id和id前是关闭的 id之后是开放的

    for k, v in ipairs(timelineSubkeyInfo) do
        local scrollListRender = DailyworkflowRender.create()
        scrollListRender:setPosition(num * 188,0)
        local isInRange = self:_judgeCurrentInWhichRange(v.time,tonumber(v.ID))
        scrollListRender:setData({idx = num, time = v.time, inTimeRange = isInRange})
        if isInRange then
            --设置当前时间区域
            self._timeRange = v
        end
        scrollView:addChild(scrollListRender)
        table.insert(self._allList,scrollListRender)
        num = num + 1

    end
    scrollView:setInnerContainerSize(cc.size(num * 188,492))
    --需要滚动到当前时间区域的前一个区域
    if self._timePoint <= 0 then
        local val = self._timePoint /(#timelineSubkeyInfo - 5) * 100
        scrollView:jumpToPercentHorizontal(val)
    else
        local val = (self._timePoint - 1) /(#timelineSubkeyInfo - 5) * 100
        scrollView:jumpToPercentHorizontal(val)
    end

    self:initTimelineNodes()
end

function ClassRef:_judgeCurrentInWhichRange(time, id)
    local serverTime = app.session:getServerDate()
    local timeArray = string.split(time,"-")
    local time1 = string.split(timeArray[1],":")
    local time2 = string.split(timeArray[2],":")
    local minTime = os.time({year =serverTime.year, month = serverTime.month, day =serverTime.day, hour =time1[1], min =time1[2]})
    local maxTime = os.time({year =serverTime.year, month = serverTime.month, day =serverTime.day, hour =time2[1], min =time2[2]})

    local serverTimeSec = app.session:getServerTime()
    if serverTimeSec >= minTime and serverTimeSec < maxTime then
        return true
    else

        if serverTimeSec >= minTime then
            self._timePoint = id
        end

        return false
    end
end

function ClassRef:initTimelineNodes()
    --读取timeline数据填充
    self._allBlockRender = {}
    local timelineInfo = GD:getTimeLineConfigData()
    for k,v in ipairs(timelineInfo) do
        local belongArray = string.split(v.belong,";")
        local render = self._allList[checknumber(belongArray[#belongArray])]
        if render then

            local curTimeRangeIdx = 0
            if self._timeRange then
                curTimeRangeIdx = tonumber(self._timeRange.ID)
            end

            local blockRender = DailyTimelineBlockRender.createWithData({belongLength = #belongArray, conf = v, curTimeRangeIdx = curTimeRangeIdx, timePoint = self._timePoint, scrollView = self._ui.ScrollView})
            render:addTimelineBlock(blockRender,v.row)
            table.insert(self._allBlockRender, blockRender)

        end
    end
end

function ClassRef:refreshDataFromServer(data)

end

function ClassRef:onOperate(cb)
    self._onOperate = cb
end

return ClassRef
